package com.yy.jhl.ui.manager.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.ui.manager.adapter.ProductAdapter;
import com.yy.jhl.ui.manager.contract.ProductListContract;
import com.yy.jhl.ui.manager.presenter.ProductListPresenter;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/20.
 */

public class ProductListActivity extends BaseActivity<ProductListPresenter> implements ProductListContract.View {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    ProductAdapter productAdapter;
    String batchId = "";
    List<ProductInfo> selectProductList;
    public static void startAction(Context mContext, String orderId,String batchId,List<ProductInfo> selectProductList,int requestCode) {
        Intent intent = new Intent(mContext, ProductListActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("batchId", batchId);
        intent.putExtra("selectProductList",(Serializable) selectProductList);
        ((Activity)mContext).startActivityForResult(intent,requestCode);
    }
    @Override
    public int getLayoutId() {
        return R.layout.act_product_list;
    }

    @Override
    public void initView() {
        selectProductList = (List<ProductInfo>)getIntent().getSerializableExtra("selectProductList");
        batchId = getIntent().getStringExtra("batchId");
        showTitle("产品列表", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        productAdapter = new ProductAdapter(mContext,true);
        listview.setAdapter(productAdapter);
        mPresenter.getList(getIntent().getStringExtra("orderId"), StringUtil.isEmpty(batchId) ? "" : batchId);
    }

    @OnClick(R.id.ensure_btn)
    public void onViewClicked() {
        if (productAdapter.getModelList().size() > 0){
            if (productAdapter.isHaveData()){
                Intent intent = new Intent();
                intent.putExtra("productList",(Serializable) productAdapter.getModelList());
                setResult(RESULT_OK,intent);
                finish();
            }else {
                showShortToast("产品数量不能为空！");
            }
        }
    }

    @Override
    public void getListSuccess(List<ProductInfo> info) {
        productAdapter.addAllData(info);
        productAdapter.setSelectList(selectProductList);

    }

    @Override
    public void getListFail() {

    }
}
